Skip to content

Auto-deref map access in assignments and Add Forward Declarations#14

Open
SiyuanSun0736 wants to merge 1 commit intomultikernel:mainfrom
SiyuanSun0736:fix_in_ringbuf_demo
Open

Auto-deref map access in assignments and Add Forward Declarations#14
SiyuanSun0736 wants to merge 1 commit intomultikernel:mainfrom
SiyuanSun0736:fix_in_ringbuf_demo

Conversation

@SiyuanSun0736
Copy link
Contributor

Overview

This PR resolves a type mismatch bug in eBPF C code generation and improves the robustness of userspace code generation. The primary fix ensures that map lookups on the right-hand side of assignment statements follow KernelScript's value semantics rather than returning raw pointers.

Key Changes

1. eBPF C Codegen: Assignment Value Semantics

  • Target: src/ebpf_c_codegen.ml
  • Change: Modified generate_assignment to use auto_deref_map_access:true when the source value is an IRMapAccess.
  • Logic: Previously, while variable declarations (e.g., var x = map[k]) correctly generated dereferencing logic, standard assignments (e.g., x = map[k]) were incorrectly assigning the bpf_map_lookup_elem pointer directly to a value-type variable.

2. Userspace Codegen: Forward Declarations

  • Target: src/userspace_codegen.ml
  • Change: Added logic to generate forward declarations for ring buffer callback handlers (e.g., int <handler>(<ValueType> *event);).
  • Logic: Prevents implicit declaration errors and order-of-definition issues in the generated userspace C code, especially when dispatch_used is true.

Code Comparison (eBPF C)

Before

Assigning a pointer type (struct Stats *) to a value type (struct Stats) caused a compilation error.

struct Stats* __map_lookup_3;
// ... (lookup logic)
stat = __map_lookup_3; // Error: incompatible type

After

The assignment now generates a safe dereferencing block with a NULL check.

struct Stats* __map_lookup_3;
// ... (lookup logic)
stat = ({ 
    struct Stats __val = {0}; 
    if (__map_lookup_3) { 
        __val = *(__map_lookup_3); 
    } 
    __val; 
});

Impact & Rationale

  • Type Safety: Enforces consistent behavior across all map access contexts (initialization vs. assignment).
  • Correctness: Fixes the incompatible type error observed in examples/ringbuf_demo.
  • Code Standards: Generated userspace code now includes necessary prototypes for callbacks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant